module.exports = {
  open:false,
  description:'更新会话',
  handler(req,res,next){
    let doc = req.body;
    let date = new Date();
    doc.createTime = date;
    let fromID = doc.fromID;
    let toID = doc.toID;
    let talkerIDArray = [fromID,toID].sort();
    req.mongo.collection("conversation").find({"talkerIDArray":talkerIDArray}).next(
        (e,r)=>{
          if(e) return res.err.mongoError(e);
          if(!r){//如果找不到会话ID
            req.mongo.collection("conversation").insertOne(
                {"talkerIDArray":talkerIDArray,"createTime":date},
                (e,r)=>{
                  if(e) return res.err.mongoError(e);
                  let conversationID = r.insertedId;
                  doc.conversationID = conversationID;//将产生的conversation的ID赋给doc
                  req.mongo.collection("message").insertOne(
                      doc,
                      (e,r)=>{
                        if(e) return res.err.mongoError(e);
                        let messageID = r.insertedId;
                        req.mongo.collection("user").updateMany(
                            {"_id":{$in:talkerIDArray}},
                            {$addToSet:{"conversationIDArray":conversationID},$set:{"lastModifyDate":date}},
                            {upsert:true},
                            (e,r)=>{
                              if(e) return res.err.mongoError(e);
                              //
                              req.mongo.collection("conversation").update(
                                  {"_id":conversationID},
                                  {$set:{"lastMessageID":messageID,"lastMessageUserId":fromID,lastMessageContent:req.body.content,lastMessageDate:date,lastMessageUser:req.body.fromNickname,lastMessageUserHeadPicPath:req.body.fromHeadPicPath,readState:0}},
                                  {upsert:true},
                                  (e,r)=>{
                                    if(e) return res.err.mongoError(e);
                                    return res.reply({"code":3000,"msg":"消息创建成功！","data":{"conversationID":conversationID}})
                                  }
                              )
                            }
                        )
                      }
                  )
                }
            )
          }else{//如果找到会话ID
            let conversationID = r._id;
            doc.conversationID = conversationID;//将查询到的conversation的ID赋给doc
            req.mongo.collection("message").insertOne(
                doc,
                (e,r)=>{
                  if(e) return res.err.mongoError(e);
                  let messageID = r.insertedId;
                  req.mongo.collection("user").updateMany(
                      {"_id":{$in:talkerIDArray}},
                      {$addToSet:{"conversationIDArray":conversationID},$set:{"lastModifyDate":date}},
                      {upsert:true},
                      (e,r)=>{
                        if(e) return res.err.mongoError(e);
                        //
                        req.mongo.collection("conversation").update(
                            {"_id":conversationID},
                            {$set:{"lastMessageID":messageID,"lastMessageUserId":fromID,lastMessageContent:req.body.content,lastMessageDate:date,lastMessageUser:req.body.fromNickname,lastMessageUserHeadPicPath:req.body.fromHeadPicPath,readState:0}},
                            {upsert:true},
                            (e,r)=>{
                              if(e) return res.err.mongoError(e);
                              return res.reply({"code":3000,"msg":"消息创建成功！","data":{"conversationID":conversationID}})
                            }

                        )
                      }
                  )
                }
            )
          }
        }
    )
  }
}